package com.jwthhealth_pub.old.sign.view;

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.jwthhealth_pub.R;
import com.jwthhealth_pub.app.App;
import com.jwthhealth_pub.app.view.MainActivity;
import com.jwthhealth_pub.common.RegularUtil;
import com.jwthhealth_pub.common.preference.PreferenceKey;
import com.jwthhealth_pub.common.view.TitleLayout;
import com.jwthhealth_pub.old.api.ApiHelper;
import com.jwthhealth_pub.old.common.Constant;
import com.jwthhealth_pub.old.individual.module.HraDate;
import com.jwthhealth_pub.old.sign.model.LoginModel;
import com.jwthhealth_pub.old.sign.presenter.ISignInPresenter;
import com.jwthhealth_pub.old.sign.presenter.ISignInPresenterCompl;
import com.jwthhealth_pub.old.signutil.model.db.UserInfoDao;
import com.jwthhealth_pub.utils.Contas;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 登陆 注册
 * Created by Administrator on 2017/1/23.
 */
public class SignInActivity extends Activity {
    public static final int SIGN_TOAST = 0;
    public static final int SIGN_TOKEN = 1;
    public static final int SEND_SMS = 2;
    public static final int ZFB_AUTH_FLAG = 3;

    public static final String FROM_HOME = "0";
    public static final String FROM_GUIDE = "1";
    public static final String FROM_INDIVIDUAL = "2";

    public static final String QQ = "0";
    public static final String WX = "1";
    public static final String WB = "2";
    public static final String ZFB = "3";

    public final String fromHome = "0";
    public final String fromGuide = "1";
    public final String fromIndividual = "2";
    public static final String fromReport = "3";

    public final String SMS_SIGNUP = "signup";
    public final String QQ_BIND = "qqbind";
    public final String WX_BIND = "wxbind";
    public final String WB_BIND = "bindweibo";
    public final String ZFB_BIND = "zfbbind";

    //    @BindView(R.id.btn_sign_in_qq)
//    TextView btnSignInQq;
//    @BindView(R.id.btn_sign_in_wx)
//    TextView btnSignInWx;
//    @BindView(R.id.btn_sign_in_wb)
//    TextView btnSignInWb;
    @BindView(R.id.input_name)
    EditText inputName;
    @BindView(R.id.input_pw)
    EditText inputPw;
    @BindView(R.id.btn_sign_in)
    Button btnSignIn;
    @BindView(R.id.btn_sign_up)
    TextView btnSignUp;
    @BindView(R.id.btn_find_pw)
    TextView btnFindPw;
    @BindView(R.id.ed_sign_up_phone_num)
    EditText edSignUpPhoneNum;
    @BindView(R.id.ed_sign_up_pw)
    EditText edSignUpPw;
    @BindView(R.id.ed_sign_up_confirm_pw)
    EditText edSignUpConfirmPw;
    @BindView(R.id.ed_verification_code)
    EditText edVerificationCode;
    @BindView(R.id.btn_get_verification_code)
    Button btnGetVerificationCode;
    @BindView(R.id.btn_sign_up_commit)
    Button btnSignUpCommit;
    @BindView(R.id.progressbar)
    ProgressBar progressbar;
    @BindView(R.id.btn_sign_up_commit_sec)
    Button btnSignUpCommitSec;
    @BindView(R.id.btn_cls)
    ImageView cancelBtn;
    @BindView(R.id.sign_topbar)
    TitleLayout signTopLayout;
    @BindView(R.id.ed_bind_code)
    EditText edCode;
    @BindView(R.id.ed_bind_phone)
    EditText edPhone;
    @BindView(R.id.btn_bind_code)
    Button btnCode;
    @BindView(R.id.btn_bind_phone)
    Button btnBindPhone;

    private View mSignInLayout, mSignUpLayout, mSignPwLayout, mSignBindPhone, mSignThiLayout;

    private ISignInPresenter mISignInPresenter;

    private String signUn;

    private static String signToken;

    private String tag;
    private UserInfoDao userInfoDao;
    private String bindPhone;
    private String bindName;
    private String bindPic;
    private String bindOpenId;
    private String bindType;

    public void setTag(String tag) {
        this.tag = tag;
    }

    public String getTag() {
        return tag;
    }

    /**
     * 用于判断是否正在发送请求
     */
    private boolean isRequesting;

    public void setRequesting(boolean requesting) {
        isRequesting = requesting;
    }

    public SignInHandler mHandler;

//    IUiListener iUiListener = new IUiListener() {
//        @Override
//        public void onComplete(Object o) {
//            Log.d("SignInActivity", "o:" + o);
//        }
//
//        @Override
//        public void onError(UiError uiError) {
//            Log.d("SignInActivity", "uiError:" + uiError);
//        }
//
//        @Override
//        public void onCancel() {
//            Log.d("SignInActivity", "canlce");
//        }
//    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
        ButterKnife.bind(this);

        mHandler = new SignInHandler(this);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        }

        // TODO: 2016/12/19 include 无法使用注入?
        mSignInLayout = findViewById(R.id.layout_sign_in);
        mSignUpLayout = findViewById(R.id.layout_sign_up);
        mSignPwLayout = findViewById(R.id.layout_sign_pw);
        mSignPwLayout = findViewById(R.id.layout_sign_pw);
        mSignBindPhone = findViewById(R.id.layout_bind_phone);
        mSignThiLayout = findViewById(R.id.include_thi_sign_in);


        //计算适配比例
//        DensityUtil.px2dip(App.mContext, 130);

//        TextChange textChange = new TextChange();
//        inputName.addTextChangedListener(textChange);
//        inputPw.addTextChangedListener(textChange);
//        edSignUpPw.addTextChangedListener(textChange);
//        edSignUpConfirmPw.addTextChangedListener(textChange);
//        edSignUpPhoneNum.addTextChangedListener(textChange);
//        edVerificationCode.addTextChangedListener(textChange);

//        View[] views = new View[]{btnSignInQq, btnSignInWx, btnSignInWb};
//        FontManager.markAsIconContainer(views);

        mISignInPresenter = new ISignInPresenterCompl(this);

        userInfoDao = UserInfoDao.getInstance();

        initClick();
    }

    private void initClick() {
        signTopLayout.setOnTitleLayoutListener(new TitleLayout.TitleLayoutListener() {
            @Override
            public void leftclick() {

                if (mSignBindPhone.getVisibility() == View.VISIBLE) {
                    mSignInLayout.setVisibility(View.VISIBLE);
                    mSignThiLayout.setVisibility(View.VISIBLE);
                    mSignBindPhone.setVisibility(View.GONE);
                    signTopLayout.setTitle(getResources().getString(R.string.sign_in_title));
                    return;
                }

                if (mSignInLayout.getVisibility() == View.VISIBLE) {
                    finish();
                } else if (mSignUpLayout.getVisibility() == View.VISIBLE) {
                    signTopLayout.setTitle(getResources().getString(R.string.sign_in_title));
                    mSignInLayout.setVisibility(View.VISIBLE);
                    mSignUpLayout.setVisibility(View.GONE);
                    mSignPwLayout.setVisibility(View.GONE);
                } else {
                    signTopLayout.setTitle(getResources().getString(R.string.sign_up_title));
                    mSignInLayout.setVisibility(View.GONE);
                    mSignUpLayout.setVisibility(View.VISIBLE);
                    mSignPwLayout.setVisibility(View.GONE);
                }
            }

            @Override
            public void leftTextClick() {

            }

            @Override
            public void rightclick() {

            }

            @Override
            public void rightTextClick() {

            }
        });
    }

    /**
     * 登陆成功
     *
     * @param data 登陆成功返回数据
     */
    public void signIn(LoginModel.DataBean data) {
        try {
            progressbar.setVisibility(View.GONE);

            if (getTag().equals(fromReport)) {

                LoginModel.DataBean dataBean5 = UserInfoDao.getInstance().queryUserinfoModel();
                if (dataBean5 == null) {
                    Toast.makeText(this, "请先登陆", Toast.LENGTH_SHORT).show();
                    return;
                }

                Call<HraDate> call = ApiHelper.getHraDate(dataBean5.getId(), dataBean5.getAndroidtoken());
                call.enqueue(new Callback<HraDate>() {
                    @Override
                    public void onResponse(Call<HraDate> call, Response<HraDate> response) {
                        final HraDate body = response.body();
                        if (body.getCode().equals("0")) {
                            if (body.getData().size() <= 0) {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        Toast.makeText(SignInActivity.this, "暂无体检数据", Toast.LENGTH_SHORT).show();
                                    }
                                });
                                return;
                            }
                            Toast.makeText(SignInActivity.this, "IndividualHealthReportActivity", Toast.LENGTH_SHORT).show();
//                            Intent intent3 = new Intent(SignInActivity.this, IndividualHealthReportActivity.class);
//                            intent3.putExtra(Constant.ISRETEXT, true);
//                            startActivity(intent3);
                            finish();
                        } else {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(SignInActivity.this, body.getMsg(), Toast.LENGTH_SHORT).show();
                                }
                            });
                        }
                    }

                    @Override
                    public void onFailure(Call<HraDate> call, Throwable t) {

                    }
                });

                Toast.makeText(this, "IndividualHealthReportActivity", Toast.LENGTH_SHORT).show();

//                Intent intent = new Intent(this, IndividualHealthReportActivity.class);
//                intent.putExtra(Constant.USER_INFO, data);
//                startActivity(intent);
                finish();
            }

            if (getTag().equals(fromGuide)) {
                LoginModel.DataBean userinfoModel = userInfoDao.queryUserinfoModel();
                if (userinfoModel == null) {
                    userInfoDao.add(data);
                }
                Contas.putObject(SignInActivity.this, data);

                Intent intent = new Intent(this, MainActivity.class);
//                intent.putExtra(Constant.USER_INFO, data);
                startActivity(intent);
                finish();
            }


            if (getTag().equals(fromGuide)) {
                Intent intent = new Intent(this, MainActivity.class);
                intent.putExtra(Constant.USER_INFO, data);
                startActivity(intent);
                finish();
            } else if (getTag().equals(fromHome)) {
                LoginModel.DataBean userinfoModel = userInfoDao.queryUserinfoModel();
                if (userinfoModel == null) {
                    userInfoDao.add(data);
                    Contas.putObject(SignInActivity.this, data);
                }
                finish();
            } else if (getTag().equals(fromIndividual)) {
                LoginModel.DataBean userinfoModel = userInfoDao.queryUserinfoModel();
                if (userinfoModel == null) {
                    boolean add = userInfoDao.add(data);
                    if (add) {
                        Log.d("SignInActivity", "add is signInSuccess");
                    } else {
                        Log.d("SignInActivity", "add isn't signInSuccess");
                    }
                } else {
                    Log.d("SignInActivity", "userinfomodel isn't null");
                }
//                startActivity(new Intent(this, IndividualActivity.class));
                finish();
            }
        } catch (Exception e) {
            Log.d("SignInActivity", e.toString());
        }
    }

    /**
     * 三方登陆成功
     *
     * @param data 登陆成功返回数据
     */
    public void signIn(HashMap<String, Object> data) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(Constant.SIGN_DATA, data);
        startActivity(intent);
    }

    public void signUp() {
        toas(getResources().getString(R.string.sign_up_hint_signup_success));
        showOptionLayout(true);
    }

    public void netErrorToast() {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(SignInActivity.this, SignInActivity.this.getResources().getString(R.string.common_net_error), Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void toas(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        progressbar();
//        Message message = new Message();
//        Bundle bundle = new Bundle();
//        bundle.putString(Constant.TOAS, msg);
//        message.setData(bundle);
//        mHandler.sendMessage(message);
    }

    /**
     * 显示&隐藏 登陆|注册 布局
     */
    public void showOptionLayout(boolean isSignIn) {
        if (isSignIn) {
            signTopLayout.setTitle(getResources().getString(R.string.sign_in_title));
            mSignInLayout.setVisibility(View.VISIBLE);
            mSignUpLayout.setVisibility(View.GONE);
            mSignPwLayout.setVisibility(View.GONE);
        } else {
            signTopLayout.setTitle(getResources().getString(R.string.sign_up_title));
            edSignUpPhoneNum.setText("");
            edSignUpPw.setText("");
            edVerificationCode.setText("");
            mSignUpLayout.setVisibility(View.VISIBLE);
            mSignInLayout.setVisibility(View.GONE);
        }
    }

    public void showSignUpOneOrTwo(boolean isStepOne) {
        if (isStepOne) {
            mSignUpLayout.setVisibility(View.VISIBLE);
            mSignPwLayout.setVisibility(View.GONE);
        } else {
            mSignUpLayout.setVisibility(View.GONE);
            mSignPwLayout.setVisibility(View.VISIBLE);
        }
    }

    public void progressbar() {
        if (progressbar.getVisibility() == View.VISIBLE) {
            progressbar.setVisibility(View.GONE);
        } else {
            progressbar.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 用于第三方登录后绑定手机
     *
     * @param nickname
     * @param pic
     * @param openid
     * @param type     0 wx， 1 qq， 2 wb, 3 zfb
     */
    public void bindPhone(String nickname, String pic, String openid, String type) {
        bindName = nickname;
        bindPic = pic;
        bindOpenId = openid;
        bindType = type;
        mSignInLayout.setVisibility(View.GONE);
        mSignUpLayout.setVisibility(View.GONE);
        mSignPwLayout.setVisibility(View.GONE);
        mSignInLayout.setVisibility(View.GONE);
        mSignThiLayout.setVisibility(View.GONE);
        mSignBindPhone.setVisibility(View.VISIBLE);
        signTopLayout.setTitle("请绑定手机");
    }

    /**
     * 登陆监听器
     */
    class TextChange implements TextWatcher {

        @Override
        public void afterTextChanged(Editable arg0) {

        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {

        }

        @Override
        public void onTextChanged(CharSequence cs, int start, int before,
                                  int count) {

            boolean Sign2 = inputName.getText().length() > 0;
            boolean Sign3 = inputPw.getText().length() > 0;

            if (Sign2) {
                cancelBtn.setVisibility(View.VISIBLE);
            } else {
                cancelBtn.setVisibility(View.GONE);
            }

            if (Sign2 & Sign3) {
                btnSignIn.setEnabled(true);
            }
            // 在layout文件中，对Button的text属性应预先设置默认值，否则刚打开程序的时候Button是无显示的
            else {
                btnSignIn.setEnabled(false);
            }

            boolean sign4 = edSignUpPw.getText().length() > 0;
            boolean sign5 = edSignUpConfirmPw.getText().length() > 0;

            if (sign4 & sign5) {
                btnSignUpCommitSec.setEnabled(true);
            } else {
                btnSignUpCommitSec.setEnabled(false);
            }

            boolean sign6 = edSignUpPhoneNum.getText().length() > 0;

            if (sign6) {
                btnGetVerificationCode.setEnabled(true);
            } else {
                btnGetVerificationCode.setEnabled(false);
            }

            int length = edVerificationCode.getText().length();
            Log.d("TextChange", "length:" + length);
            boolean sign7 = length > 0;
            if (sign6 && sign7) {
                btnSignUpCommit.setEnabled(true);
            } else {
                btnSignUpCommit.setEnabled(false);
            }
        }

    }

    public class SignInHandler extends Handler {
        private final WeakReference<SignInActivity> mActivity;
        private SignInActivity mRealActivity;

        SignInHandler(SignInActivity activity) {
            mActivity = new WeakReference<>(activity);
            this.mRealActivity = activity;
        }

        @Override
        public void handleMessage(Message msg) {
            SignInActivity activity = mActivity.get();
            if (activity != null) {
                switch (msg.what) {
                    case SIGN_TOAST:
                        String toas = msg.getData().getString(Constant.TOAS);
                        Toast.makeText(mRealActivity, toas, Toast.LENGTH_SHORT).show();
                        mRealActivity.showOptionLayout(true);
                        activity.progressbar.setVisibility(View.GONE);
                        break;
                    case SIGN_TOKEN:
                        signToken = msg.getData().getString(Constant.SIGN_TOKEN);
                        mRealActivity.showSignUpOneOrTwo(false);
                        activity.progressbar.setVisibility(View.GONE);
                        break;
                    case SEND_SMS:
                        String message = msg.getData().getString(Constant.SIGN_TOKEN);
                        Toast.makeText(activity, message, Toast.LENGTH_SHORT).show();
                        break;
                    case ZFB_AUTH_FLAG:
//                        //支付宝登陆
//                        AuthResult authResult = new AuthResult((Map<String, String>) msg.obj, true);
//                        String resultStatus = authResult.getResultStatus();
//                        if (TextUtils.equals(resultStatus, "9000") && TextUtils.equals(authResult.getResultCode(), "200")){
//                            final String result = authResult.getAuthCode();
//                            Log.d("PayDemoActivity", result);
//
////                            String mac = "";
////                            // 获取wifi管理器
////                            WifiManager wifiMng = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
////                            WifiInfo wifiInfor = wifiMng.getConnectionInfo();
////                            mac = wifiInfor.getMacAddress();
//
//                            String mac = App.preferenceUtil.getString(Constant.MAC_ADDRESS, null);
//                            String sign = MD5Util.getMD5(mac + "android" + result);
//
//                            RequestBody requestBody = new MultipartBody.Builder()
//                                    .setType(MultipartBody.FORM)
//                                    .addFormDataPart("dev", "android")
//                                    .addFormDataPart("mac", mac)
//                                    .addFormDataPart("code", result)
//                                    .addFormDataPart("sign", sign)
//                                    .build();
//
//                            Request post = new Request.Builder()
//                                    .url(ApiHelper.BASE_URL+"login/zfb")
//                                    .method("POST", RequestBody.create(null, new byte[0]))
//                                    .post(requestBody)
//                                    .build();
//
//                            Log.d("SignInHandler", ApiHelper.BASE_URL + "login/zfb");
//                            OkHttpClient okHttpClient = new OkHttpClient();
//                            okHttpClient.newCall(post).enqueue(new okhttp3.Callback() {
//                                @Override
//                                public void onFailure(okhttp3.Call call, IOException e) {
//                                    Toast.makeText(App.mContext, "网络错误", Toast.LENGTH_SHORT).show();
//                                    Log.e("PayDemoActivity", call.toString());
//                                }
//
//                                @Override
//                                public void onResponse(okhttp3.Call call, okhttp3.Response response) throws IOException {
//                                    try {
//                                        JSONObject resultJson = new JSONObject(response.body().string());
//                                        String code = resultJson.getString("code");
//                                        if (code.equals("0")) {
//                                            App.preferenceUtil.putBoolean(PreferenceKey.CACHE_TOKEN, true);
//
//                                            LoginModel.DataBean data = new LoginModel.DataBean();
//                                            JSONObject dataJson = resultJson.getJSONObject("data");
//                                            data.setId(dataJson.getString("id"));
//                                            data.setUsername(dataJson.getString("username"));
//                                            data.setEmail(dataJson.getString("email"));
//                                            data.setPhone(dataJson.getString("phone"));
//                                            data.setTruename(dataJson.getString("truename"));
//                                            data.setIdcard(dataJson.getString("idcard"));
//                                            data.setAge(dataJson.getString("age"));
//                                            data.setAddress(dataJson.getString("address"));
//                                            data.setCity(dataJson.getString("city"));
//                                            data.setProvince(dataJson.getString("province"));
//                                            data.setCompany(dataJson.getString("company"));
//                                            data.setSex(dataJson.getString("sex"));
//                                            data.setHeight(dataJson.getString("height"));
//                                            data.setWeight(dataJson.getString("weight"));
//                                            data.setRole(dataJson.getString("role"));
//                                            data.setIostoken(dataJson.getString("iostoken"));
//                                            data.setAndroidtoken(dataJson.getString("androidtoken"));
//                                            data.setStatus(dataJson.getString("status"));
//                                            data.setGroupname(dataJson.getString("groupname"));
//
//                                            setRequesting(false);
//                                            signIn(data);
//                                        } if (code.equals("2")) {
//                                            final JSONObject data = resultJson.getJSONObject("data");
//                                            runOnUiThread(new Runnable() {
//                                                @Override
//                                                public void run() {
//                                                    try {
//                                                        bindPhone(data.getString("nicname"), data.getString("pic"), data.getString("openid"), "3");
//                                                    } catch (Exception e) {
//                                                        Log.e("SignInHandler", e.toString());
//                                                    }
//                                                }
//                                            });
//                                        } else {
//                                            Toast.makeText(SignInActivity.this, resultJson.getString("msg"), Toast.LENGTH_SHORT).show();
//                                        }
//                                    } catch (Exception e) {
//                                        Log.d("SignInHandler", e.toString());
//                                    }
//                                }
//                            });
//                        } else {
//                            Toast.makeText(App.mContext,
//                                    "授权失败" + String.format("authCode:%s", authResult.getAuthCode()), Toast.LENGTH_SHORT).show();
//                        }
                        break;
                    default:
                        break;
                }
            }

            super.handleMessage(msg);
        }
    }

    //    @OnClick({R.id.btn_cls, R.id.btn_sign_in_qq, R.id.btn_sign_in_wx, R.id.btn_sign_in_wb, R.id.btn_sign_in, R.id.btn_sign_in_zfb, R.id.btn_sign_up, R.id.btn_find_pw, R.id.btn_get_verification_code, R.id.btn_sign_up_commit, R.id.btn_sign_up_commit_sec, R.id.btn_bind_code, R.id.btn_bind_phone})
    @OnClick({R.id.btn_cls, R.id.btn_sign_in, R.id.btn_sign_up, R.id.btn_find_pw, R.id.btn_get_verification_code, R.id.btn_sign_up_commit, R.id.btn_sign_up_commit_sec, R.id.btn_bind_code, R.id.btn_bind_phone})
    public void onClick(View view) {
        switch (view.getId()) {

            case R.id.btn_cls:
                inputName.setText("");
                break;
            case R.id.btn_sign_in:
                String username = inputName.getText().toString().trim();
                if (username.length() <= 0) {
                    Toast.makeText(this, R.string.sign_in_alert_un, Toast.LENGTH_SHORT).show();
                    break;
                }
                String pw = inputPw.getText().toString().trim();
                if (pw.length() <= 0) {
                    Toast.makeText(this, R.string.sign_in_alert_pw, Toast.LENGTH_SHORT).show();

                    break;
                }

                if (isRequesting) {
                    break;
                }

                progressbar.setVisibility(View.VISIBLE);
                mISignInPresenter.signIn(username, pw);
                isRequesting = true;
                break;
            case R.id.btn_sign_up:
                showOptionLayout(false);
                break;
            case R.id.btn_sign_up_commit:
                signUn = edSignUpPhoneNum.getText().toString().trim();
                String code = edVerificationCode.getText().toString().trim();

                if (!RegularUtil.checkPhoneNumber(signUn)) {
                    Toast.makeText(this, R.string.sign_up_alert_phone, Toast.LENGTH_SHORT).show();
                    break;
                }
                if (code.length() <= 0) {
                    Toast.makeText(this, R.string.sign_find_pw_testing_hint, Toast.LENGTH_SHORT).show();
                    break;
                }

                if (isRequesting) {
                    break;
                }

                isRequesting = true;
                mISignInPresenter.signUp(signUn, code);
                break;
            case R.id.btn_find_pw:
                startActivity(new Intent(this, SignPwActivity.class));
                break;
//            case R.id.btn_sign_in_qq:
//                mISignInPresenter.signIn(QQ);
//                break;
//            case R.id.btn_sign_in_wx:
//                mISignInPresenter.signIn(WX);
//                break;
//            case R.id.btn_sign_in_wb:
//                mISignInPresenter.signIn(WB);
//                break;
//            case R.id.btn_sign_in_zfb:
//                mISignInPresenter.signIn(ZFB);
//            break;
            case R.id.btn_sign_up_commit_sec:
                String setpw = edSignUpPw.getText().toString().trim();
                String correntPw = edSignUpConfirmPw.getText().toString().trim();
                if (setpw.equals(correntPw)) {
                    if (isRequesting) {
                        break;
                    }
                    isRequesting = true;
                    mISignInPresenter.signUp(signUn, correntPw, signToken);
                } else {
                    Toast.makeText(this, R.string.sign_up_alert_pw_inconformity, Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.btn_get_verification_code:
//                Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();
                String phone = edSignUpPhoneNum.getText().toString().trim();
                mISignInPresenter.reqeustCode(phone, SMS_SIGNUP);
                break;
            case R.id.btn_bind_code:
                //请求验证码
                Toast.makeText(this, "验证码已发送", Toast.LENGTH_SHORT).show();
                try {

                    bindPhone = edPhone.getText().toString().trim();
                    boolean isPhoneNum = RegularUtil.checkPhoneNumber(bindPhone);
                    if (isPhoneNum) {
                        switch (bindType) {
                            case "0":
                                mISignInPresenter.reqeustCode(bindPhone, WX_BIND);
                                break;
                            case "1":
                                mISignInPresenter.reqeustCode(bindPhone, QQ_BIND);
                                break;
                            case "2":
                                mISignInPresenter.reqeustCode(bindPhone, WB_BIND);
                                break;
                            case "3":
                                mISignInPresenter.reqeustCode(bindPhone, ZFB_BIND);
                                break;
                            default:
                                break;
                        }
                    } else {
                        Toast.makeText(this, "请输入正确的手机号码", Toast.LENGTH_SHORT).show();
                    }
                } catch (Exception e) {
                    Log.d("SignInActivity", e.toString());
                }
                break;
            case R.id.btn_bind_phone:
                //绑定手机号
                String bindCode = edCode.getText().toString().trim();
                if (bindCode.length() == 6) {
//                    String name = App.preferenceUtil.getString(PreferenceKey.WXNAME, "");
//                    String pic = App.preferenceUtil.getString(PreferenceKey.WXPIC, "");
//                    String openid = App.preferenceUtil.getString(PreferenceKey.WXOPENID, "");
//                    Log.d("SignInActivity", pic);
                    Log.d("SignInActivity", bindType);
                    mISignInPresenter.bindPhoneNum(bindName, bindPic, bindPhone, bindCode, bindOpenId, bindType);
                } else {
                    toas(getResources().getString(R.string.code_length_error));
                }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d("SignInActivity", "requestCode:" + requestCode);

//        App.mTencent.onActivityResultData(requestCode, resultCode, data, iUiListener);

//        if(requestCode == SyncStateContract.Constants.REQUEST_API) {
//            if(resultCode == Constants.REQUEST_LOGIN) {
//                App.mTencent.handleLoginData(data, iUiListener);
//            }
//            super.onActivityResult(requestCode, resultCode, data);
//        }


//        Tencent.onActivityResultData(requestCode, resultCode, data, new IUiListener() {
//            @Override
//            public void onComplete(Object o) {
//                Log.d("SignInActivity", "completer");
//            }
//
//            @Override
//            public void onError(UiError uiError) {
//                Log.d("SignInActivity", "error");
//            }
//
//            @Override
//            public void onCancel() {
//                Log.d("SignInActivity", "cancel");
//            }
//        });

//        SsoHandler ssoHandler = mISignInPresenter.getmSsoHandler();
//        if (ssoHandler != null&&data!=null) {
//            ssoHandler.authorizeCallBack(requestCode, resultCode, data);
//        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        boolean isWxLogin = App.preferenceUtil.getBoolean(PreferenceKey.ISWXLOGIN, false);
        if (isWxLogin) {
            App.preferenceUtil.putBoolean(PreferenceKey.ISWXLOGIN, false);
            final String name = App.preferenceUtil.getString(PreferenceKey.WXNAME, "");
            final String pic = App.preferenceUtil.getString(PreferenceKey.WXPIC, "");
            final String openid = App.preferenceUtil.getString(PreferenceKey.WXOPENID, "");

            Log.d("SignInActivity", pic);

//            Call<LoginModel> thiLoginModuleCall = ApiHelper.SignInThi(name, pic, openid, "0");
//            thiLoginModuleCall.enqueue(new Callback<LoginModel>() {
//                @Override
//                public void onResponse(Call<LoginModel> call, Response<LoginModel> response) {
//                    final LoginModel body = response.body();
//                    String code = body.getCode();
//                    if (code.equals("0")) {
//                        App.preferenceUtil.putBoolean(PreferenceKey.CACHE_TOKEN, true);
//                        LoginModel.DataBean data = body.getData();
//                        signIn(data);
//                    } else {
//                        bindPhone(name, pic, openid, "0");
//                    }
//                }
//
//                @Override
//                public void onFailure(Call<LoginModel> call, Throwable t) {
//                    runOnUiThread(new Runnable() {
//                        @Override
//                        public void run() {
//                            Toast.makeText(SignInActivity.this, "网络错误", Toast.LENGTH_SHORT).show();
//                        }
//                    });
//                }
//            });
        }
    }
}
